const express = require('express')
const router = express.Router();
const db = require('../../utils/db')

router.get('/xjh/manage/coupon', (req, res) => {
    const sql = `SELECT * FROM discounts_management AS t1 JOIN USER AS t2 ON t1.dis_operator=t2.user_id WHERE 1;`;
    db(sql, null, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: '失败' })
        } else {
            res.send({ error: 0, msg: '数据成功', data: data })
        }
    })
})
router.get('/xjh/manage/coupon/change', (req, res) => {
    if (req.query.del == 'release') {
        const sql = `UPDATE discounts_management SET dis_publish_state ='已生效' WHERE dis_id=${req.query.dis_id}`
        db(sql, null, (err, data) => {
            if (err) {
                console.log(err)
                res.send({ error: 1, msg: "发布失败" })
            } else {
                res.send({ error: 0, msg: '发布成功', data: data })
            }
        })
    } else if (req.query.del == 'rel') {
        const sqla = `UPDATE discounts_management SET dis_publish_state ='未生效' WHERE dis_id=${req.query.dis_id}`
        db(sqla, null, (err, data) => {
            if (err) {
                console.log(err)
                res.send({ error: 1, msg: "下架失败" })
            } else {
                res.send({ error: 0, msg: '下架成功', data: data })
            }
        })
    } else if (req.query.del == 'refer') {
        const sqlb = `UPDATE  discounts_management SET dis_name ='${req.query.dis_name}',dis_type = '${req.query.dis_type}',dis_money=${req.query.dis_money},dis_conditions='${req.query.dis_conditions}',dis_time='${req.query.dis_time}',dis_amount=${req.query.dis_amount},dis_publish_state='${req.query.dis_publish_state}',dis_effctive_state='${req.query.dis_effctive_state}',dis_note='${req.query.dis_note}' WHERE dis_id=${req.query.dis_id};    `
        db(sqlb, (err, data) => {
            if (err) {
                console.log(err)
                res.send({ error: 1, msg: "编辑失败" })
            } else {
                res.send({ error: 0, msg: '编辑成功', data: data })
            }
        })
    } else if (req.query.del == 'del') {
        console.log(11111);
        const sqlc = `DELETE FROM discounts_management WHERE dis_id=${req.query.dis_id};`
        db(sqlc, null, (err, data) => {
            if (err) {
                res.send({ error: 1, msg: "删除失败" })
            } else {
                res.send({ error: 0, msg: '删除成功', data: data })
            }
        })
    }


})



module.exports = router